<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>后台管理系统</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="../bootstrap-4.6.2/css/bootstrap.min.css"/>
    <script src="../jquery/jquery-3.6.0.min.js"></script>
    <script src="../bootstrap-4.6.2/js/bootstrap.min.js"></script>
    <style>
        *{
            padding: 0;
            margin: 0 ;
        }
        .w{
            width: 1200px;
            margin: 0 auto;
            background-color: pink;
        }
        .box{
            border-bottom: 1px solid gray;
        }
        .title{
            text-align: center;
        }
        ul{
            margin: 0;
            padding: 0;
        }
        .envent{
            height: 50px;
            line-height: 50px;
            text-align: center;
        }
        .envent button{
            display: block;
            margin: 0 auto;
        }
        .btnList ul li{
            display: inline-block;
            margin: 5px 10px;
            float: left;
        }
    </style>
</head>
<body>
 <div class="box">
     <div class="title">
         <h2>添加事件</h2>
     </div>
 </div>
  <div class="envent">
      <button  class="btn  btn-primary"  data-toggle="modal" data-target="#exampleModal">添加事件</button>
  </div>
   <div  style="margin: 20px 40px">
       <div class="btnList">
           <ul>
               <li><button onclick="getEventList()"  class="btn btn-primary">投票开始事件</button></li>
               <li> <button onclick="getEventList2()"  class="btn btn-primary">投票未开始事件</button></li>
               <li> <button  onclick="getEventList3()"  class="btn btn-primary">资金筹集统计</button></li>
               <li> <button  onclick="getIncomeList()"  class="btn btn-primary">收入清单</button></li>
               <li> <button  onclick="getOutEventList()"  class="btn btn-primary">支出清单</button></li>
               <li><a  class="btn btn-primary" href="../index.jsp">退出登陆</a></li>
           </ul>


       </div>
       <table  class="table table-hover">
           <tbody id="eventList">

           </tbody>
       </table>
   </div>


 <!-- Modal 模态框-->
 <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
     <div class="modal-dialog">
         <div class="modal-content">
             <div class="modal-header">
                 <h5 class="modal-title" id="exampleModalLabel">班费筹集事件添加</h5>
                 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                 </button>
             </div>
             <div class="modal-body">
                 <form>
                     <div class="form-group">
                         <label for="eName">事件名称:</label>
                         <input type="text" class="form-control"  placeholder="" id="eName">
                     </div>
                     <div class="form-group">
                         <label for="epay">预算筹集</label>
                         <input type="number" class="form-control" id="epay" placeholder="">
                     </div>
                     <div class="form-group">
                         <label for="grade">年纪:</label>
                         <select class="form-control" id="grade">

                         </select>
                     </div>
                     <div class="form-group">
                         <label for="clazz">班级:</label>
                         <select class="form-control" id="clazz">
                         </select>
                     </div>
                     <div class="form-group">
                         <label for="sDate">开始时间</label>
                         <input type="datetime-local" class="form-control" id="sDate" placeholder="">
                     </div>
                     <div class="form-group">
                         <label for="eDate">结束时间</label>
                         <input type="datetime-local" class="form-control" id="eDate" placeholder="">
                     </div>
                 </form>
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                 <button type="button"  onclick="addEvent()" class="btn btn-primary">添加</button>
             </div>
         </div>
     </div>
 </div>
<script>
   getEventList2();
    getGrade();
    getClazz();
   function getIncomeList() {
       $.ajax({
           url: "http://localhost:8080/getIncomeList",
           type: "POST",
           dataType: "json",
           success: function (json) {
               if (json.code == 200) {
                   $("#eventList").empty();
                   $("#eventList").append(" <tr> <td>" +
                       "收入事由</td> <td>" +
                       "收入金额</td> <td>" +
                       "收入时间</td></tr>");
                   for (let i = 0; i < json.data.length; i++) {
                       const event = json.data[i];
                       $("#eventList").append(
                           "<tr> <td>" + event.in_name +
                           "</td> <td>" + event.in_money+
                           "</td> <td>" + event.in_date +
                           "</td> </tr>");
                   }
               } else {
                   alert(json.msg);
               }
           }
       });
   }
   function getOutEventList() {
       $.ajax({
           url: "http://localhost:8080/getOutEventList",
           type: "POST",
           dataType: "json",
           success: function (json) {
               if (json.code == 200) {
                   $("#eventList").empty();
                   $("#eventList").append(" <tr> <td>" +
                       "支出事由</td> <td>" +
                       "支出金额</td> <td>" +
                       "支出时间</td></tr>");
                   for (let i = 0; i < json.data.length; i++) {
                       const event = json.data[i];
                       $("#eventList").append(
                           "<tr> <td>" + event.pay_name +
                           "</td> <td>" + event.pay_money+
                           "</td> <td>" + event.pay_date +
                           "</td> </tr>");
                   }
               } else {
                   alert(json.msg);
               }
           }
       });
   }
    function getGrade() {
        $.ajax({
            url: "http://localhost:8080/getAllGradeServlet",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#grade").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        const grade = json.data[i];
                        $("#grade").append("<option value="+grade.gid+">" + grade.gname + "</option>");
                    }
                }
            }
        });
    }
    function getClazz() {
        $.ajax({
            url: "http://localhost:8080/getAllClazzServlet",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#clazz").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        const clazz = json.data[i];
                        $("#clazz").append("<option>" + clazz.cName + "</option>");
                    }
                }
            }
        });
    }
    function addEvent() {
       let eName=  $("#eName").val();
       let ePay=$("#epay").val();
       let sDate= $("#sDate").val();
        let eDate= $("#eDate").val();
       let grade= $("#grade").val();
        let clazz= $("#clazz").val();
        console.log(sDate);
        console.log(eDate);
        $.ajax({
            url: "http://localhost:8080/addEventServlet",
            type: "POST",
            data:{
                ename:eName,
                epay:ePay,
                sdate:sDate,
                edate:eDate,
                grade:grade,
                clazz:clazz,
            },
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    alert(json.msg);
                    $('#exampleModal').modal('hide')
                    location.reload();
                }
            }
        });
    }
    function getEventList(){
        $.ajax({
            url: "http://localhost:8080/selectEventList",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        let isStart= "";
                        let isOk="";
                        const event= json.data[i];
                        if (event.isDoing===1){
                           isStart="投票已开始";
                        }else  {
                            isStart="投票未开始";
                        }
                        if (event.isOk===0){
                          isOk="不成立";
                        }else {
                            isOk="成立";
                        }
                        console.log(event.isDoing);
                        $("#eventList").append("<tr><td>" +event.eName+
                            "</td><td>" +event.ePay+
                            "</td><td>" +event.eNum+
                            "</td><td>" +event.sTime+
                            "</td><td>" +event.eTime+
                            "</td><td>" +isOk+
                            "</td><td>" +isStart+
                            "</td><td>" +event.supNum+
                            "</td><td>" +event.noSup+
                            "</td><td>" +event.cid+
                            "</td><td>" +event.totalPay+
                            "</td><td>" +"<button disabled onclick='beginSupport("+event.eid+")' class='btn btn-primary'>开始投票</button>"+
                            "</td></tr>");
                    }
                }else {
                    alert(json.msg);
                }
            }
        });
    }
    function getEventList2(){
        $.ajax({
            url: "http://localhost:8080/selectEventList2",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        let isStart= "";
                        let isOk="";
                        const event= json.data[i];
                        if (event.isDoing===1){
                            isStart="投票已开始";
                        }else  {
                            isStart="投票未开始";
                        }
                        if (event.isOk===0){
                            isOk="不成立";
                        }else {
                            isOk="成立";
                        }
                        console.log(event.isDoing);
                        $("#eventList").append("<tr><td>" +event.eName+
                            "</td><td>" +event.ePay+
                            "</td><td>" +event.eNum+
                            "</td><td>" +event.sTime+
                            "</td><td>" +event.eTime+
                            "</td><td>" +isOk+
                            "</td><td>" +isStart+
                            "</td><td>" +event.supNum+
                            "</td><td>" +event.noSup+
                            "</td><td>" +event.cid+
                            "</td><td>" +event.totalPay+
                            "</td><td>" +"<button  onclick='beginSupport("+event.eid+")' class='btn btn-primary'>开始投票</button>"+
                            "</td></tr>");
                    }
                }else {
                    alert(json.msg);
                }
            }
        });
    }
   function getEventList3(){
       $.ajax({
           url: "http://localhost:8080/getIsOkEvent",
           type: "POST",
           dataType: "json",
           success: function (json) {
               if (json.code == 200) {
                   $("#eventList").empty();
                   for (let i = 0; i < json.data.length; i++) {
                       const event= json.data[i];
                       let estatus="";
                       if (event.totalPay>=event.ePay){
                            estatus="已完成筹集!"
                       }else {
                           estatus="正在筹集中!"
                       }
                       $("#eventList").append("<tr><td>" +event.eName+
                           "</td><td>" +event.eName+
                           "</td><td>" +event.totalPay+
                           "</td><td>" +estatus+
                           "</td><td>" +event.eTime+
                           "</td><td>" +"<button  onclick='getEventList4("+event.eid+")' class='btn btn-primary'>查看筹集资金清单</button>"+
                           "</td></tr>");
                   }
               }else {
                   alert(json.msg);
               }
           }
       });
   }
   function getEventList4(eid){
       $.ajax({
           url: "http://localhost:8080/getEventInfoStu",
           type: "POST",
           data:{eid:eid},
           dataType: "json",
           success: function (json) {
               if (json.code == 200) {
                   $("#eventList").empty();
                   for (let i = 0; i < json.data.length; i++) {
                       const event= json.data[i];
                       $("#eventList").append(
                           "<tr><td>" +event.e_name+
                           "</td><td>" +event.s_name+
                           "</td><td>" +event.pay+
                           "</td><td>" +event.pdate+
                           "</td></tr>");
                   }
               }else {
                   alert(json.msg);
               }
           }
       });
   }
    function  beginSupport(eid){
        $.ajax({
            url: "http://localhost:8080/supportStart",
            type: "POST",
            data:{eid:eid},
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                   alert(json.msg);
                   location.reload();
                }else {
                    alert(json.msg);
                }
            }
        });
    }

</script>
</body>
</html>
